"""
########################################
文件描述：清楚测试环境IM群聊和用户
########################################
"""

from common.com_logging import logout
from common.com_excel import Excel
import json
import requests


class ImClear:

    table = Excel('r', './im需删除的群聊手机号和组.xlsx')
    p_row = table.read_row('phone')
    g_row = table.read_row('group')
    logout('info', '需删除的手机号总计：%s\t需删除的聊天组总计：%s' % (p_row, g_row))

    def clear_phone(self):

        for i in range(self.p_row):
            phone = self.table.row_value('phone', i)
            # {"Accounts":["18861176689"]}
            headers = {'Content-Type': 'application/json'}
            data = {"Accounts": phone}
            data = json.dumps(data)
            response = requests.post(url='http://hxfim.zhyinfo.cn/Api/IM_User/DeleteAccounts',
                                     data=data,
                                     headers=headers,
                                     timeout=20)
            cont = response.text
            if '"Status":100' in cont:
                logout('info', '删除第%s个手机号：%s成功\n请求响应：%s' % (i+1, phone, cont))
            else:
                logout('error', '删除第%s个手机号：%s失败\n请求响应：%s' % (i+1, phone, cont))

    def clear_group(self):

        for j in range(self.g_row):
            group = self.table.row_value('group', j)[0]
            headers = {'Content-Type': 'application/json'}
            data = {"GroupId": group}
            data = json.dumps(data)
            response = requests.post(url='http://hxfim.zhyinfo.cn/Api/IM_Group/DestroyGroup',
                                     data=data,
                                     headers=headers,
                                     timeout=20)

            cont = response.text
            if '"Status":100' in cont:
                logout('info', '删除第%s个群组：%s成功\n请求响应：%s' % (j + 1, group, cont))
            else:
                logout('error', '删除第%s个群组：%s失败\n请求响应：%s' % (j + 1, group, cont))


if __name__ == '__main__':
    ic = ImClear()
    ic.clear_group()
    ic.clear_phone()